var e = getApp();
var session = require('../../../utils/session.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    array:[],
    canIUse: wx.canIUse("button.open-type.getUserInfo"),
    isHidden: !0,
    app_name: e.system.app_name,
    image_prefix: e.system.image_prefix,
    imei: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
  },

  gotohome: function() {
    wx.reLaunch({
      url: '../../home/index/index',
    })
  },

  gotoscan: function() {
    wx.scanCode({
      success(res) {
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    e.getUserDevice()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  formSubmit: function (e) {
    var t = e.detail.value.imei.trim();
    t ? this.bindImeiToUser(t) : wx.showModal({
      title: "提示",
      content: "请输入设备号",
      showCancel: !1,
      success: function (e) { }
    });
  },

  bindImeiToUser: function (t) {
    let openId = wx.getStorageSync("wechatOpenid")
    wx.showLoading({}) 
    wx.request({
      url: e.system.app_url_prefix + "/weapp/userinfo/bind?openId=" + openId + "&code=" + t,
      method: "POST",
      data: {},
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (e) {
        200 === e.statusCode ? e.data && 0 === parseInt(e.data.code) ? wx.reLaunch({
          url: "../../home/index/index?imei=" + t
        }) : wx.showModal({
          title: "提示",
            content: e.data.msg,
          showCancel: !1,
          success: function (e) { }
        }) : wx.showModal({
          title: "提示",
          content: "添加设备失败",
          showCancel: !1,
          success: function (e) { }
        });
      },
      fail: function (e) { },
      complete: function (e) {
        wx.hideLoading();
      }
    });
  },
})